import request from "@/utils/request";

const SOURCE_BASE_URL = "/api/v1/source";

const DbSourceAPI = {
  /** 获取分页数据 */
  getPage(queryParams?: DbSourcePageQuery) {
    return request<any, PageResult<DbSourcePageQuery[]>>({
      url: `${SOURCE_BASE_URL}/page`,
      method: "get",
      params: queryParams,
    });
  },

  /** 获取下拉数据源 */
  getOptions() {
    return request<any, OptionType[]>({
      url: `${SOURCE_BASE_URL}/options`,
      method: "get",
    });
  },

  /**
   * 获取表单数据
   *
   * @param id 主键ID
   * @returns 表单数据
   */
  getFormData(id: string) {
    return request<any, DbSourceForm>({
      url: `${SOURCE_BASE_URL}/${id}/form`,
      method: "get",
    });
  },

  /** 添加数据 */
  create(data: DbSourceForm) {
    return request({
      url: `${SOURCE_BASE_URL}`,
      method: "post",
      data,
    });
  },

  /**
   * 更新表单
   *
   * @param id 主键ID
   * @param data 表单数据
   */
  update(id: string, data: DbSourceForm) {
    return request({
      url: `${SOURCE_BASE_URL}/${id}`,
      method: "put",
      data,
    });
  },

  /**
   * 批量删除，多个以英文逗号(,)分割
   *
   * @param ids 主键ID字符串，多个以英文逗号(,)分割
   */
  deleteByIds(ids: string) {
    return request({
      url: `${SOURCE_BASE_URL}/${ids}`,
      method: "delete",
    });
  },
};

export default DbSourceAPI;

/** 分页查询参数 */
export interface DbSourcePageQuery extends PageQuery {
  /** 搜索关键字 */
  keywords?: string;
  status?: number;
}

/** 分页对象 */
export interface DbSourcePageVO {
  id?: string;
  name?: string;
  dbName?: string;
  userName?: string;
  jdbcUrl?: string;
  status?: number;
  createTime?: Date;
}

/** 表单对象 */
export interface DbSourceForm {
  id?: string;
  name?: string;
  dbName?: string;
  userName?: string;
  password?: string;
  jdbcUrl?: string;
  status?: number;
}
